package com.zatech.cgnci.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.MessageQueryDTO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.service.CgnciMessageService;
import com.zatech.cgnci.project.vo.MessageCountVO;
import com.zatech.cgnci.project.vo.MessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author user
 */
@RestController
@RequestMapping("/api/message")
@RequiredArgsConstructor
@Api(tags = "消息中心接口")
public class CgnciMessageController {

    private final CgnciMessageService messageService;
    
    @PostMapping("/page")
    @ApiOperation("分页查询消息")
    public IPage<MessageVO> page(@RequestBody MessageQueryDTO queryDTO) {
        UserEntity user = UserContext.getUser();
        if(VerifyUtils.isEmpty(user)){
            return null;
        }
        return messageService.pageMessage(queryDTO);
    }
    
    @GetMapping("/detail/{messageId}")
    @ApiOperation("获取消息详情")
    public MessageVO detail(@PathVariable Long messageId) {
        UserEntity user = UserContext.getUser();
        if(VerifyUtils.isEmpty(user)){
            return null;
        }
        return messageService.getDetail(messageId);
    }
    
    @PostMapping("/read")
    @ApiOperation("标记消息已读")
    public void read(@RequestBody List<Long> messageIds) {
        messageService.markAsRead(messageIds);
    }
    
    @DeleteMapping("/clear")
    @ApiOperation("清空全部消息")
    public void clear() {
        messageService.clearAll();
    }
    
    @GetMapping("/count")
    @ApiOperation("获取消息统计")
    public MessageCountVO count() {
        UserEntity user = UserContext.getUser();
        if(VerifyUtils.isEmpty(user)){
            return null;
        }
        return messageService.getCount();
    }
} 